package com.xiong.bistu.wetchat;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xiong.bistu.wetchat.API.WetChatPayApiCall;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PayActivity extends Activity {

	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay);

		api = WXAPIFactory.createWXAPI(this, "wx426b3015555a46be");

		Button appayBtn = (Button) findViewById(R.id.appay_btn);
		appayBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				WetChatPayApiCall.apiGetPayInfo().enqueue(new Callback<WetChatPayInfo>() {
					@Override
					public void onResponse(Call<WetChatPayInfo> call, Response<WetChatPayInfo> response) {
						if(response.body()!=null&&response.body().code==0){
							WetChatPayInfo.PayDate payDate = response.body().getPayDate();
							Toast.makeText(PayActivity.this,"OK",Toast.LENGTH_SHORT).show();
							Toast.makeText(PayActivity.this,""+payDate.getAppid(),Toast.LENGTH_SHORT).show();
							PayReq req = new PayReq();
							//req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
							req.appId			= payDate.getAppid();
							req.partnerId		= payDate.getPartnerid();
							req.prepayId		= payDate.getPrepayid();
							req.nonceStr		= payDate.getNoncestr();
							req.timeStamp		= payDate.getTimestamp();
							req.packageValue	= payDate.getMypackage();
							req.sign			= payDate.getSign();
							req.extData			= "app data"; // optional
							Log.e("Xiong","appId——"+req.appId);
							Log.e("Xiong","partnerId——"+req.partnerId);
							Log.e("Xiong","prepayId——"+req.prepayId);
							Log.e("Xiong","nonceStr——"+req.nonceStr);
							Log.e("Xiong","timeStamp——"+req.timeStamp);
							Log.e("Xiong","packageValue——"+req.packageValue);
							Log.e("Xiong","sign——"+req.sign);
							Log.e("Xiong","extData——"+req.extData);

							Toast.makeText(PayActivity.this, "req.partnerId："+req.partnerId, Toast.LENGTH_SHORT).show();
							Toast.makeText(PayActivity.this, "正常调起支付", Toast.LENGTH_SHORT).show();
							// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
							api.sendReq(req);
						}
					}

					@Override
					public void onFailure(Call<WetChatPayInfo> call, Throwable t) {
						Toast.makeText(PayActivity.this,"服务器请求失败",Toast.LENGTH_SHORT).show();
					}
				});

				//region Description
//				String url = "http://47.93.163.33:8086/ajax/test";
//				Button payBtn = (Button) findViewById(R.id.appay_btn);
//				payBtn.setEnabled(false);
//				Toast.makeText(PayActivity.this, "获取订单中...", Toast.LENGTH_SHORT).show();
//				try{
//					byte[] buf = Util.httpGet(url);
//					if (buf != null && buf.length > 0) {
//						String content = new String(buf);
//						Log.e("get server pay params:",content);
//						JSONObject json = new JSONObject(content);
//						if(null != json && !json.has("retcode") ){
//							PayReq req = new PayReq();
//							//req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
//							req.appId			= json.getString("appid");
//							req.partnerId		= json.getString("partnerid");
//							req.prepayId		= json.getString("prepayid");
//							req.nonceStr		= json.getString("noncestr");
//							req.timeStamp		= json.getString("timestamp");
//							req.packageValue	= json.getString("package");
//							req.sign			= json.getString("sign");
//							req.extData			= "app data"; // optional
//							Toast.makeText(PayActivity.this, "req.partnerId："+req.partnerId, Toast.LENGTH_SHORT).show();
//							Toast.makeText(PayActivity.this, "正常调起支付", Toast.LENGTH_SHORT).show();
//							// 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
//							api.sendReq(req);
//						}else{
//							Log.d("PAY_GET", "返回错误"+json.getString("retmsg"));
//							Toast.makeText(PayActivity.this, "返回错误"+json.getString("retmsg"), Toast.LENGTH_SHORT).show();
//						}
//					}else{
//						Log.d("PAY_GET", "服务器请求错误");
//						Toast.makeText(PayActivity.this, "服务器请求错误", Toast.LENGTH_SHORT).show();
//					}
//				}catch(Exception e){
//					Log.e("PAY_GET", "异常："+e.getMessage());
//					Toast.makeText(PayActivity.this, "异常："+e.getMessage(), Toast.LENGTH_SHORT).show();
//				}
//				payBtn.setEnabled(true);
				//endregion
			}
		});
		Button checkPayBtn = (Button) findViewById(R.id.check_pay_btn);
		checkPayBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean isPaySupported = api.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
				Toast.makeText(PayActivity.this, String.valueOf(isPaySupported), Toast.LENGTH_SHORT).show();
			}
		});
	}

}
